﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace VsSourceCpoy.Helper
{
    public class TxtFileHelper
    {
        /// <summary>
        /// 读取一个文件中的所有行
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static List<string> ReadAllLines(string path, GetLineFilter filter)
        {
            List<string> usableStringList = new List<string>();
            try
            {
                using (StreamReader fs = new StreamReader(path))
                {
                    string line = null;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (filter != null && !filter.KeepLine(ref line))
                        {
                            continue;
                        }
                        usableStringList.Add(line);
                    }
                    return usableStringList;
                }
            }
            catch
            {
                return usableStringList;
            }
        }

        public static List<string> ReadUntil(string path, GetLineFilter filter,Func<string,int,bool> until)
        {
            List<string> usableStringList = new List<string>();
            try
            {
                using (StreamReader fs = new StreamReader(path))
                {
                    string line = null;
                    int lineCount = 0;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (filter != null && !filter.KeepLine(ref line))
                        {
                            continue;
                        }
                        lineCount++;
                        usableStringList.Add(line);
                        if (until(line, lineCount))
                            break;
                    }
                    return usableStringList;
                }
            }
            catch
            {
                return usableStringList;
            }
        }

        /// <summary>
        /// 把allLines中的数据全部写到文件中，一条数据一行
        /// </summary>
        /// <param name="path"></param>
        /// <param name="allLines"></param>
        public static void WriteAllLines(string path, List<string> allLines)
        {
            List<string> usableStringList = new List<string>();
            File.WriteAllLines(path, usableStringList.ToArray());
        }
    }

    /// <summary>
    /// 指定读取文件方式
    /// </summary>
    public class GetLineFilter
    {
        private bool _deleteEmptyLine = false;
        /// <summary>
        /// 是否删除空行
        /// </summary>
        public bool DeleteEmptyLine { get { return _deleteEmptyLine; } set { _deleteEmptyLine = value; } }

        private bool _trimLine = false;
        /// <summary>
        /// 是否去除空白
        /// </summary>
        public bool TrimLine { get { return _trimLine; } set { _trimLine = value; } }

        private Func<string, bool> _commentFilter;
        /// <summary>
        /// 用于注释判断
        /// </summary>
        public Func<string, bool> CommentFilter
        {
            get
            {
                _commentFilter = _commentFilter ?? DefalutCommentFilter;
                return _commentFilter;
            }
            set
            {
                _commentFilter = value;
            }
        }

        private static bool DefalutCommentFilter(string line)
        {
            line = line ?? "";
            return line.Trim().StartsWith("//");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="line"></param>
        /// <returns>true:继续本行，false：忽略本行</returns>
        public bool KeepLine(ref string line)
        {
            line = line ?? "";
            if (TrimLine)
            {
                line = line.Trim();
            }
            if (DeleteEmptyLine && string.IsNullOrEmpty(line))
            {
                return false;
            }
            if (CommentFilter(line))
            {
                return false;
            }
            return true;
        }
    }
}
